﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Xml;
using System.Collections.Generic;

using ESF.General;

#endregion


namespace ESF.Object.XmlRepresenter.Rules
{
	public sealed class OnlyOneRule : IEquatable <OnlyOneRule>
	{
		#region Atributos
		
		private string _Name;
		private string _UniqueValue;
		
		private XmlMember _Member;
		
		#endregion
		
		
		#region Propiedades
		
		public string Name { get { return this._Name; } }
		
		public XmlMember Member { get { return this._Member; } }
		
		public string UniqueValue { get { return this._UniqueValue; } }
		
		#endregion
		
				
		#region Constructor
		// TODO: Crear la regla de al menos uno de xyz requerido uno.
		// TODO: Ajustar la mecanica de OnlyOneRule
		public OnlyOneRule (ref XmlObject pobjObject, ref XmlTextReader pxmlReader)
		{
			XmlHelper xmlEx;
			
			
			xmlEx = new XmlHelper ();
			xmlEx.EvaluatedNodeReader = pxmlReader;
			xmlEx.ObjectName = "OnlyOneRule";
			
			this._Name = xmlEx.GetAttributeValue (true, "name", "Nombre");
			this._UniqueValue = xmlEx.GetAttributeValue (true, "uniqueValue", "Valor Único");
			
			this._Member = pobjObject [xmlEx.GetAttributeValue (true, "member", "Nombre del Miembro")];
			
			xmlEx.Validate.VerifyValueType (true, "El valor unico", this._Member.Type.Value, this._UniqueValue);
		}
		
		#endregion

		
		#region Funciones
		
		public void ToXml (ref XmlTextWriter pxmlWriter)
		{
			pxmlWriter.WriteStartElement ("OnlyOne");
						
			pxmlWriter.WriteAttributeString ("name", this._Name);
			pxmlWriter.WriteAttributeString ("member", this._Member.Name);
			pxmlWriter.WriteAttributeString ("uniqueValue", this._UniqueValue);
			
			pxmlWriter.WriteEndElement ();
		}
		
		#endregion


		#region IEquatable<OnlyOneRule> Members

		public bool Equals (OnlyOneRule pobjOther)
		{
			return (this._Name == pobjOther.Name);
		}

		#endregion
	}
}